{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_test=[1,2,3,4,5,6,7]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算几何平均数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def geometric_mean(data):\n",
    "    total=1\n",
    "    for i in data:\n",
    "        total*=i\n",
    "    return pow(total,1/len(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.3800151591412964\n"
     ]
    }
   ],
   "source": [
    "print(geometric_mean(data_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.41934406893907 —— 15.11111111111111\n",
      "4.528728688116765 —— 5.5\n",
      "(3.0452884314245487+1.466533615522254j) —— 3.7142857142857144\n",
      "3.3800151591412964 —— 3.142857142857143\n",
      "0.0 —— 3.5\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "print(geometric_mean([1,2,3,4,5,6,7,8,100]),'——',numpy.mean([1,2,3,4,5,6,7,8,100]))\n",
    "print(geometric_mean([1,2,3,4,5,6,7,8,9,10]),'——',numpy.mean([1,2,3,4,5,6,7,8,9,10]))\n",
    "print(geometric_mean([-1,2,3,4,5,6,7]),'——',numpy.mean([-1,2,3,4,5,6,7]))\n",
    "print(geometric_mean([-1,-2,3,4,5,6,7]),'——',numpy.mean([-1,-2,3,4,5,6,7]))\n",
    "print(geometric_mean([0,1,2,3,4,5,6,7]),'——',numpy.mean([0,1,2,3,4,5,6,7]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
